package cn.bsb.controller.rece;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;

import cn.bsb.pojo.DevUser;
import cn.bsb.service.rece.LoginService;
import cn.bsb.tools.Constants;


@Controller
public class loginControllerrece {
	@Autowired
	private LoginService loginService;

	@RequestMapping("/Login")
	public String Login() {
		System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa");
		return "rece/login";
	}
	
	@RequestMapping(value="login", method = RequestMethod.POST)
	public String Index(String user_code,String userPassword,HttpSession session, Model attr) {
		DevUser user = loginService.loginUser(user_code, userPassword);
		if(user == null) {
			attr.addAttribute("message", "网页开小差了!");
			return "rece/login";
		}
		session.setAttribute("loginUser", user);
		return "redirect:articlelist";
	}

	@RequestMapping("logout")
	public String logout(HttpSession session) {
		session.removeAttribute("loginUser");
		return "redirect:articlelist";
	}
	//注册
			@RequestMapping(value="/register",method=RequestMethod.GET)
			public  String register(@ModelAttribute("devUser") DevUser devUser/*,@PathVariable String iString*/){
				
				
				return "rece/register";
			}
			@RequestMapping(value="/register",method=RequestMethod.POST)
			public String doregister(DevUser devUser,BindingResult bindingResult,
					@RequestParam("a_idPicPath") MultipartFile attach,HttpServletRequest request){
				
				String idPicPath=null;
				
				
				if (!attach.isEmpty()) {///brainstormingblogs/src/main/webapp/statics/rece/images
					String path=request.getSession().getServletContext().getRealPath("statics/rece/fileUpload");
					System.out.println(path);
					//得到原文件的名称
					String oldFileName=attach.getOriginalFilename();
					
					//得到原文件的后缀
					String prefix=FilenameUtils.getExtension(oldFileName);
					//设置一下上传文件的大小
					int fileSize=500000;//(大约500k)
					if (attach.getSize()>fileSize) {
						request.setAttribute("uploadFileError", "大小不要超过500kb");
						return "rece/register";
					} else {
						//设置文件后缀格式
						if (prefix.equalsIgnoreCase("jpg")||
								prefix.equalsIgnoreCase("png")||
								prefix.equalsIgnoreCase("gif")) {
							//将要保存的文件名
							String fileName=System.currentTimeMillis()+RandomUtils.nextInt(100000)+"_myImg."+prefix;
							File targetFile=new File(path,fileName);
							if (!targetFile.exists()) {
								targetFile.mkdirs();
							}
							try {
								attach.transferTo(targetFile);
								
								
								idPicPath=path+File.separator+fileName;
							} catch (IllegalStateException e) {
								e.printStackTrace();
							} catch (IOException e) {
								e.printStackTrace();
							}
			
						} else {
							request.setAttribute("uploadFileError", "上传文件类型必须为：jpg、gif、png");
							return "rece/register";
						}
					}
					
					
				}
				
				devUser.setPic(idPicPath);
				devUser.setCreattime(new Date());
				int result=loginService.addUser(devUser);
				if (result>0) {
					return "rece/login";
				} else {
					
					return "rece/register";
				}
			}

			//用户编码的校验
			@RequestMapping(value="ucexist",produces= {"application/json;charset=UTF-8"})
			@ResponseBody
			public Object ucexist(@RequestParam("user_code")String user_code,HttpSession session) {
				DevUser dUser=loginService.getUserCodeByuser_Code(user_code);
				
				Map<String, String> resultMap = new HashMap<String, String>();
				
					
					if(dUser!=null){
						resultMap.put("user_code", "exist");
					}else if(user_code.equals("")) {
						resultMap.put("user_code", "existEmpty");
					}else{
						resultMap.put("user_code", "noexist");
					}
					
				
				return JSONArray.toJSON(resultMap);
			}		
}
